package com.tanhua.dubbo.api.mongo;

import com.tanhua.domain.mongo.FollowUser;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

/**
 * 小视频用户关注服务接口实现类
 */
@Service
public class FollowUserApiImpl implements FollowUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 视频用户关注
     */
    @Override
    public void saveFollowUser(FollowUser followUser) {
        //1.查看当前用户是否已经关注此用户

        //2.查询被关注用户 是否 关注了当前用户

        //3.如果互相关注 则成为好友tanhua_users

        //4.如果没有互相关注，直接操作follow_user
        followUser.setId(ObjectId.get());
        followUser.setCreated(System.currentTimeMillis());
        mongoTemplate.save(followUser);

    }

    /**
     * 视频用户取消关注
     */
    @Override
    public void removeFollowUser(FollowUser followUser) {
        Query query = new Query();
        query.addCriteria(
                Criteria.where("userId").is(followUser.getUserId())
                        .and("followUserId").is(followUser.getFollowUserId()));
        mongoTemplate.remove(query, FollowUser.class);
    }
}
